//
//  UIButton+UIActivityIndicatorView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/30.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "UIButton+UIActivityIndicatorView.h"

static NSInteger UIButtonActivityIndicatorViewTag = 10000;

@implementation UIButton (UIActivityIndicatorView)


- (void)showIndicator {
    [self setUserInteractionEnabled:false];
    self.titleLabel.alpha = 0;
    self.imageView.alpha = 0;
    
    UIActivityIndicatorView *indicatorView = [self viewWithTag:UIButtonActivityIndicatorViewTag];
    if (!indicatorView) {
        indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        indicatorView.tag = UIButtonActivityIndicatorViewTag;
        [self addSubview:indicatorView];
        indicatorView.hidesWhenStopped = true;
        [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
    }
    indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [indicatorView startAnimating];
}
- (void)hiddenIndicator {
    [self setUserInteractionEnabled:true];
    self.titleLabel.alpha = 1;
    self.imageView.alpha = 1;
    UIActivityIndicatorView *indicatorView = [self viewWithTag:UIButtonActivityIndicatorViewTag];
    if (indicatorView) {
        [indicatorView removeFromSuperview];
    }
}

@end
